<template>
  <div class="login-container" >
    <el-card class="login-card">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-width="80px">
        <h2 class="login-title">用户登录</h2>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login" class="login-btn">登录</el-button>
          <el-button type="primary" @click="router.push('/reg')" class="register-btn">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { login } from '@/api/login.js';
import img from '@/assets/login-background.jpg';
import { useRouter } from 'vue-router';

export default {
  data() {
    return {
      img: img,
      router: useRouter(),
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          login(this.loginForm).then(res => {
            if (res.data.code != 200) {
              this.$message.error(res.data.msg);
            } else {
              this.$message.success('登录成功！');
              this.$router.push('/shouYe');
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
/* 重置默认样式 */
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}
#App {
  background: url("@/assets/login-background.jpg");
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100%;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.login-card {
  width: 400px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
}

.login-btn, .register-btn {
  width: 100%;
  margin-top: 10px;
}
</style>
